import env from "./env.js"
import Vue from 'vue'; //引入vue
import store from "../store/store.js"
//必须实例化 Vue
let vm = new Vue();
function req(obj) {
	let app = getApp()
	return new Promise((resolve, reject) => {
		let token = uni.getStorageSync('token')
		let url = env.BASE_API + obj.url
		let data = obj.data
		let method = obj.method || "GET"
		uni.showLoading({
			title: '加载中',
			mask:true
		})
		uni.request({
			url: url, //请求地址
			data: data,
			method: method,
			header: {
				'content-type': 'application/json', // 默认值
				"Authorization": token
			},
			success(res) {
				uni.hideLoading()
				if (res.data.code == 200) {
					resolve(res.data)
				} else if (res.data.code === 401) {
					uni.navigateTo({
						url: '/pageUser/pages/my/login'
					})
				} else {
					uni.showToast({
						title: res.data.message,
						icon: 'error',
						duration: 3000
					})
				}
			},
			fail(err) {
				uni.showToast({
					title: "数据请求失败！",
					icon: 'error',
					duration: 3000
				})
				reject(err)
			},
		})
	})
}

export default req
